/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.manager;

import br.com.foxline.enums.Status;
import br.com.foxline.model.Enterprise;
import br.com.foxline.model.Lot;
import br.com.foxline.model.ValueInterestParcel;
import br.com.foxline.service.LotService;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.*;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author luciano
 */
@ManagedBean
@ViewScoped
public class ManagerMap implements Serializable{
    
    
    private String map;
    @EJB
    private LotService lotService;
    private DecimalFormat decimalFormat = new DecimalFormat("##,###,###,##0.00", new DecimalFormatSymbols(new Locale("pt", "BR")));
    
    private String block;
    private String num;
    private String path;
    private final long ID = 11L;
     @PostConstruct
    public void init() {
       decimalFormat.setMaximumFractionDigits(2);
       decimalFormat.setParseBigDecimal(true);

       Enterprise e = new Enterprise();
       e.setId(ID);
       List<Lot> lots = lotService.getLotToMap(e);
       String resp = "{},";
       
        for (Lot lot : lots) {
            resp += "{ title: 'Quadra " + lot.getBlock() + " - Lote: " + lot.getNum() + "',"
                    + " id: '" + lot.getPathId() 
                    + "', color:'" + getColor(lot.getStatus()) 
                    + "', titleClass: '" + lot.getStatus().getStyle()
                    + "', descriptionWindowX: '1060" 
                    + "', status: '" + lot.getStatus().getName()  
                    + "', description: '"+ getDescriptionFromLote(lot) + "'},";
        }
       
        resp = resp.substring(0, resp.length() - 1);
        map = resp;
    }
    
    public String getDescriptionFromLote(Lot lot){
        StringBuilder resp = new StringBuilder();
        resp.append("Quadra : ");
        resp.append(lot.getBlock());
        resp.append(" - Número: ");
        resp.append(lot.getNum());
        resp.append("<br/>");
        resp.append("Dimensão: ");
        resp.append(dimensaoDoLote(lot));
        resp.append("<br/>");
        resp.append("Área: ");
        resp.append(lot.getArea());
        resp.append("<br/> ");
        resp.append("Tipo: ");
        resp.append(lot.getTypeLot().getName());
        if(lot.getStatus().equals(Status.DISPONIVEL)){
            resp.append("<br/><span class=\"bold\">Valores</span>");
            resp.append("<br/> ");
            resp.append("A Vista: R$");
            resp.append(decimalFormat.format(lot.getSees()));
            resp.append("<br/>Parcelado<br/>");
            resp.append(parcelas(lot));
            resp.append("<br/><br/> ");
            resp.append("<a class=\"salelink\" href=\"createsale.xhtml?pathId=" + lot.getPathId() + "\" > Comprar </a>");
        }
        return resp.toString();
    }
    
    
    public void atualizaLot(){
      FacesContext context = FacesContext.getCurrentInstance();
      Map map = context.getExternalContext().getRequestParameterMap();
      path = (String) map.get("pathLote");
    }
    
    public void save(){
        Enterprise e = new Enterprise();
        e.setId(ID);
        Integer n = Integer.parseInt(num);
        Lot l = lotService.getLot(e, block, n);
        l.setPathId(path);
        lotService.update(l);
    }
    
   public String dimensaoDoLote(Lot lot){
        if(lot == null || lot.getId() == null){
            return "";
        }
        if(lot.getDimessionX() != null && lot.getDimessionY()!= null){
            return lot.getDimessionX() + " X " + lot.getDimessionY();
        }else{
            return "VARIÁVEL";
        }
    }
    

    public String getColor(Status status){
        if(status.equals(Status.DISPONIVEL)){
            return "#65c960";
        }else if(status.equals(Status.RESERVADO)){
            return "#FFCC33";
        }else if(status.equals(Status.TRAMITE)){
            return "#0000cc";
        }else if(status.equals(Status.VENDIDO)){
            return "#db2815";
        }
        return "#888888";
    }
    
    public String getMap() {
        return map;
    }

    public void setMap(String map) {
        this.map = map;
    }
    
      public String parcelas(Lot lot) {
        StringBuffer result = new StringBuffer();
          try {
            if (lot != null) {
                List<ValueInterestParcel> parcelas = orderParcel(lot.getValueInterestParcels());
                for (ValueInterestParcel valueInterestParcel : parcelas) {
                result.append(valueInterestParcel.getInterestParcel().getQtd());
                result.append(" x ");
                result.append(decimalFormat.format(valueInterestParcel.getValue()));
                result.append("<br/>");
                }
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    public List<ValueInterestParcel> orderParcel(List<ValueInterestParcel> parcelas) {
        Collections.sort(parcelas, new Comparator<ValueInterestParcel>() {

            public int compare(ValueInterestParcel v1, ValueInterestParcel v2) {
                return Integer.compare(v1.getInterestParcel().getQtd(), v2.getInterestParcel().getQtd());
            }
        });
        return parcelas;
    }

    public String getBlock() {
        return block;
    }

    public void setBlock(String block) {
        this.block = block;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }
    
    
    
    
}
